<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">

      <el-form-item label="物流单号" prop="odd">
        <el-input
          v-model="queryParams.odd"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流公司" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入物流公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发件人联系电话" prop="ftel">
        <el-input
          v-model="queryParams.ftel"
          placeholder="请输入发件人联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="收件人联系电话" prop="stel">
        <el-input
          v-model="queryParams.stel"
          placeholder="请输入收件人联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="家电名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入家电名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="logisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物流单号" align="center" prop="odd" />
      <el-table-column label="物流公司" align="center" prop="company" />
      <el-table-column label="发货地址" align="center" prop="faddress" />
      <el-table-column label="发件人" align="center" prop="fname" />
      <el-table-column label="发件人联系电话" align="center" prop="ftel" />
      <el-table-column label="收货地址" align="center" prop="saddress" />
      <el-table-column label="收件人" align="center" prop="sname" />
      <el-table-column label="收件人联系电话" align="center" prop="stel" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="家电名称" align="center" prop="name" />
      <el-table-column label="家电图片" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="createTime" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物流配送对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单信息" prop="tid">
          <el-input v-model="form.tid" placeholder="请输入订单信息" />
        </el-form-item>
        <el-form-item label="店员/店长" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入店员/店长" />
        </el-form-item>
        <el-form-item label="物流单号" prop="odd">
          <el-input v-model="form.odd" placeholder="请输入物流单号" />
        </el-form-item>
        <el-form-item label="物流公司" prop="company">
          <el-input v-model="form.company" placeholder="请输入物流公司" />
        </el-form-item>
        <el-form-item label="发货地址" prop="faddress">
          <el-input v-model="form.faddress" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="发件人" prop="fname">
          <el-input v-model="form.fname" placeholder="请输入发件人" />
        </el-form-item>
        <el-form-item label="发件人联系电话" prop="ftel">
          <el-input v-model="form.ftel" placeholder="请输入发件人联系电话" />
        </el-form-item>
        <el-form-item label="收货地址" prop="saddress">
          <el-input v-model="form.saddress" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="收件人" prop="sname">
          <el-input v-model="form.sname" placeholder="请输入收件人" />
        </el-form-item>
        <el-form-item label="收件人联系电话" prop="stel">
          <el-input v-model="form.stel" placeholder="请输入收件人联系电话" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="家电名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入家电名称" />
        </el-form-item>
        <el-form-item label="家电图片">
          <image-upload v-model="form.pic"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLogistics, getLogistics, delLogistics, addLogistics, updateLogistics } from "@/api/system/logistics";

export default {
  name: "Logistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流配送表格数据
      logisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tid: null,
        uid: null,
        odd: null,
        company: null,
        faddress: null,
        fname: null,
        ftel: null,
        saddress: null,
        sname: null,
        stel: null,
        state: null,
        name: null,
        pic: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物流配送列表 */
    getList() {
      this.loading = true;
      listLogistics(this.queryParams).then(response => {
        this.logisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tid: null,
        uid: null,
        odd: null,
        company: null,
        faddress: null,
        fname: null,
        ftel: null,
        saddress: null,
        sname: null,
        stel: null,
        state: null,
        createBy: null,
        createTime: null,
        name: null,
        pic: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流配送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLogistics(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流配送";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLogistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLogistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物流配送编号为"' + ids + '"的数据项？').then(function() {
        return delLogistics(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/logistics/export', {
        ...this.queryParams
      }, `logistics_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
